﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 药品信息管理
{
    public partial class YP_LJTZCZ : Form
    {
        public static string currow,str_tjlx,str_pwmc,str_pwdw,str_bz,time_ZDRQ, time_PWRQ;
        public static int state_flag;
        public YP_LJTZCZ()
        {
            InitializeComponent();
        }
        DataTable dt = new DataTable();

        private void edit()
        {
                SqlConnection conn = new SqlConnection(SQL.connectDB);
                conn.Open();

                SqlCommand sc_f = new SqlCommand("SELECT FLAG FROM YP_LJTZCZ WHERE TJDH='" + dataGridViewX1.Rows[dataGridViewX1.CurrentCell.RowIndex].Cells[0].Value.ToString() + "'", conn);//sql语句
                if (Convert.ToInt16(sc_f.ExecuteScalar()) == 2)
                    state_flag = 2;
                if (Convert.ToInt16(sc_f.ExecuteScalar()) == 1)
                    state_flag = 1;
                if (Convert.ToInt16(sc_f.ExecuteScalar()) == 0)
                    state_flag = 0;
                currow = dataGridViewX1.Rows[dataGridViewX1.CurrentCell.RowIndex].Cells[0].Value.ToString();
                str_tjlx = dataGridViewX1.Rows[dataGridViewX1.CurrentCell.RowIndex].Cells[8].Value.ToString();
                str_pwmc = dataGridViewX1.Rows[dataGridViewX1.CurrentCell.RowIndex].Cells[9].Value.ToString();
                str_pwdw = dataGridViewX1.Rows[dataGridViewX1.CurrentCell.RowIndex].Cells[10].Value.ToString();
                str_bz = dataGridViewX1.Rows[dataGridViewX1.CurrentCell.RowIndex].Cells[7].Value.ToString();
                time_ZDRQ = dataGridViewX1.Rows[dataGridViewX1.CurrentCell.RowIndex].Cells[1].Value.ToString();
                time_PWRQ = dataGridViewX1.Rows[dataGridViewX1.CurrentCell.RowIndex].Cells[11].Value.ToString();
                YP_LJTZCZD ljtzczd = new YP_LJTZCZD();
                ljtzczd.Show();
                currow = null;
        }

        private void change_color(int i)
        {
            if(i==0)
                dataGridViewX1.Rows[dataGridViewX1.CurrentCell.RowIndex].DefaultCellStyle.ForeColor = System.Drawing.Color.Black;
            if (i == 1)
                dataGridViewX1.Rows[dataGridViewX1.CurrentCell.RowIndex].DefaultCellStyle.ForeColor = System.Drawing.Color.Blue;
            
            if (i == 2)
                dataGridViewX1.Rows[dataGridViewX1.CurrentCell.RowIndex].DefaultCellStyle.ForeColor = System.Drawing.Color.Green;

        }

        private void state_color(int i)
        {
            if (Convert.ToInt16(dataGridViewX1.Rows[i].Cells[6].Value) == 1)
                dataGridViewX1.Rows[i].DefaultCellStyle.ForeColor = System.Drawing.Color.Blue;
            
            if (Convert.ToInt16(dataGridViewX1.Rows[i].Cells[6].Value) == 2)
                dataGridViewX1.Rows[i].DefaultCellStyle.ForeColor = System.Drawing.Color.Green;

        }

        private void btn_new_Click(object sender, EventArgs e)
        {
            YP_LJTZCZD ljtzczd = new YP_LJTZCZD();
            ljtzczd.Show();
        }

        private void btn_refresh_Click(object sender, EventArgs e)
        {
            string str_flag="";
            if (cb_wsh.Checked == true)//未审
            {
                if (cb_ysh.Checked == true || cb_yjz.Checked==true)
                str_flag =str_flag+ " AND FLAG=0";
                else str_flag = " AND FLAG=0";
            }
            if (cb_ysh.Checked == true)//已审
            {
                if (cb_wsh.Checked == false && cb_yjz.Checked == true)
                    str_flag = str_flag + " AND FLAG=1";
                if (cb_wsh.Checked == true)
                    str_flag = str_flag + " OR FLAG=1";
                else str_flag = " AND FLAG=1";
            }
            if (cb_yjz.Checked == true)//记账
            {
                if (cb_wsh.Checked == true || cb_ysh.Checked == true)
                    str_flag = str_flag + " OR FLAG=2";
                else str_flag = " AND FLAG=2";
            }
            if (cb_wsh.Checked == false && cb_ysh.Checked == false && cb_yjz.Checked == false)
                str_flag = " AND FLAG IS NULL";
            try
            {
                dt.Clear();
                using (SqlConnection con = new SqlConnection(SQL.connectDB))
                {
                    SqlDataAdapter da = new SqlDataAdapter();//实例化sqldataadpte
                    SqlCommand cmd1 = new SqlCommand("SELECT * FROM YP_LJTZCZ WHERE RQ BETWEEN '" + dateTimeInput1.Value + "' AND '" + dateTimeInput2.Value + "' AND(1=1 " + str_flag+")", con);//sql语句
                    da.SelectCommand = cmd1;//设置为已实例化SqlDataAdapter的查询命令
                    da.Fill(dt);
                    dataGridViewX1.DataSource = dt;

                    dataGridViewX1.Columns[0].HeaderText = "调价单号";
                    dataGridViewX1.Columns[1].HeaderText = "录入日期";
                    dataGridViewX1.Columns[2].HeaderText = "录入人员";
                    dataGridViewX1.Columns[3].HeaderText = "审核人员";
                    dataGridViewX1.Columns[4].HeaderText = "记账人员";
                    dataGridViewX1.Columns[5].HeaderText = "记账日期";
                    dataGridViewX1.Columns[6].Visible = false;
                    dataGridViewX1.Columns[7].Visible = false;
                    dataGridViewX1.Columns[8].Visible = false;
                    dataGridViewX1.Columns[9].Visible = false;
                    dataGridViewX1.Columns[10].Visible = false;
                    dataGridViewX1.Columns[11].Visible = false;

                    for (int i = 0; i < dataGridViewX1.RowCount; i++)
                            state_color(i);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        #region 删除
        private void btn_del_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con_sc = new SqlConnection(SQL.connectDB);
                con_sc.Open();

                SqlCommand sc_f = new SqlCommand("SELECT FLAG FROM YP_LJTZCZ WHERE TJDH='" + dataGridViewX1.Rows[dataGridViewX1.CurrentCell.RowIndex].Cells[0].Value.ToString() + "'", con_sc);//sql语句

                if (Convert.ToInt16(sc_f.ExecuteScalar()) == 2)
                    MessageBox.Show("该冲账单号已记账,不能删除!", "提示");

                else
                {
                    SqlCommand del = new SqlCommand("DELETE YP_LJTZCZ WHERE TJDH='" + dataGridViewX1.Rows[dataGridViewX1.CurrentCell.RowIndex].Cells[0].Value.ToString() + "'", con_sc);//sql语句
                    SqlCommand delmx = new SqlCommand("DELETE FROM YP_LJTZCZD WHERE TJDH='" + dataGridViewX1.Rows[dataGridViewX1.CurrentCell.RowIndex].Cells[0].Value.ToString() + "'", con_sc);
                    delmx.ExecuteNonQuery();
                    del.ExecuteNonQuery();
                    dt.Rows.RemoveAt(dataGridViewX1.CurrentCell.RowIndex);
                    MessageBox.Show("删除成功！");
                    dataGridViewX1.DataSource = dt;
                }
            }
            catch 
            {
               
            }


        }
        #endregion
        private void btn_edit_Click(object sender, EventArgs e)
        {
            try
            {
                edit();
            }
            catch { }
        }

        private void dataGridViewX1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                edit();
            }
            catch { }
        }

        private void YP_LJTZCZ_Load(object sender, EventArgs e)
        {
            dateTimeInput1.Value = DateTime.Today;
            dateTimeInput2.Value = DateTime.Today;
            cb_wsh.Checked = true;
            cb_ysh.Checked = true;
            cb_yjz.Checked = true;
        }
        #region 审核
        private void btn_sh_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con_sh = new SqlConnection(SQL.connectDB);
                con_sh.Open();
                                
                SqlCommand sc_f = new SqlCommand("SELECT FLAG FROM YP_LJTZCZ WHERE TJDH='" + dataGridViewX1.Rows[dataGridViewX1.CurrentCell.RowIndex].Cells[0].Value.ToString() + "'", con_sh);//sql语句

                if (Convert.ToInt16(sc_f.ExecuteScalar()) == 2)
                    MessageBox.Show("该冲账单号已记账,不能更改!","提示");
                if (Convert.ToInt16(sc_f.ExecuteScalar()) == 1)
                    MessageBox.Show("该冲账单号已审核!", "提示");
                if (Convert.ToInt16(sc_f.ExecuteScalar()) == 0)
                {
                    SqlCommand sc_sh = new SqlCommand("UPDATE YP_LJTZCZ SET FLAG=1,SHRY='" + LoginForm.username + "' WHERE TJDH='" + dataGridViewX1.Rows[dataGridViewX1.CurrentCell.RowIndex].Cells[0].Value.ToString() + "'", con_sh);//sql语句
                    sc_sh.ExecuteNonQuery();
                    dataGridViewX1.CurrentRow.Cells[3].Value = LoginForm.username;
                    change_color(1);
                    MessageBox.Show("审核成功！");
                    dataGridViewX1.DataSource = dt;
                }
            }
            catch 
            {
                
            }
        }
        #endregion
        #region 记账
        private void btn_JZ_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con_sh = new SqlConnection(SQL.connectDB);
                con_sh.Open();

                SqlCommand sc_f = new SqlCommand("SELECT FLAG FROM YP_LJTZCZ WHERE TJDH='" + dataGridViewX1.Rows[dataGridViewX1.CurrentCell.RowIndex].Cells[0].Value.ToString() + "'", con_sh);//sql语句
                if(Convert.ToInt16(sc_f.ExecuteScalar()) == 0)
                    MessageBox.Show("请先审核!", "提示");
                if(Convert.ToInt16(sc_f.ExecuteScalar()) == 2)
                    MessageBox.Show("该冲账单号已经记账!", "提示");
                if (Convert.ToInt16(sc_f.ExecuteScalar()) == 1)
                {
                    string sqlyj = "UPDATE YP_LJTZCZ SET FLAG=2,JZRQ='" + DateTime.Today + "',JZRY='" + LoginForm.username + "' WHERE TJDH='" + dataGridViewX1.Rows[dataGridViewX1.CurrentCell.RowIndex].Cells[0].Value.ToString() + "'";
                    SqlCommand sc_jz = new SqlCommand(sqlyj, con_sh);//sql语句
                    //改药品字典表里单价
                    string sqlgj="UPDATE ZD_YP SET BZDJ=DJ1 WHERE BM=(SELECT YPBM  FROM  YP_LJTZCZD WHERE TJDH='" + dataGridViewX1.Rows[dataGridViewX1.CurrentCell.RowIndex].Cells[0].Value.ToString() + "')";
                    SqlCommand sc_tj = new SqlCommand(sqlgj, con_sh);
                    sc_tj.ExecuteNonQuery();
                    sc_jz.ExecuteNonQuery();
                    dataGridViewX1.CurrentRow.Cells[4].Value = LoginForm.username;
                    dataGridViewX1.CurrentRow.Cells[5].Value = DateTime.Today;
                    change_color(2);
                    MessageBox.Show("记账成功！");
                    dataGridViewX1.DataSource = dt;
                }
            }
            catch 
            {
                
            }
        }
        #endregion
        #region 消审
        private void buttonItem1_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con_xs = new SqlConnection(SQL.connectDB);
                con_xs.Open();

                SqlCommand sc_f = new SqlCommand("SELECT FLAG FROM YP_LJTZCZ WHERE TJDH='" + dataGridViewX1.Rows[dataGridViewX1.CurrentCell.RowIndex].Cells[0].Value.ToString() + "'", con_xs);//sql语句

                if (Convert.ToInt16(sc_f.ExecuteScalar()) == 2)
                    MessageBox.Show("该冲账单号已记账,不能更改!", "提示");
                if (Convert.ToInt16(sc_f.ExecuteScalar()) == 0)
                    MessageBox.Show("该冲账单号还没审核!", "提示");
                if (Convert.ToInt16(sc_f.ExecuteScalar()) == 1)
                {
                    SqlCommand sc_xs = new SqlCommand("UPDATE YP_LJTZCZ SET FLAG=0,SHRY='' WHERE TJDH='" + dataGridViewX1.Rows[dataGridViewX1.CurrentCell.RowIndex].Cells[0].Value.ToString() + "'", con_xs);//sql语句
                    sc_xs.ExecuteNonQuery();
                    dataGridViewX1.CurrentRow.Cells[3].Value = "";
                    change_color(0);
                    MessageBox.Show("消审成功！");
                    dataGridViewX1.DataSource = dt;
                }
            }
            catch 
            {

            }
        #endregion
        }
    }
}